<?php
/**
 * Created by 张彪.
 * User: 小马出行
 * Date: 2018/4/3
 * Time: 14:52
 */
use my\JoDES;
use my\Tools;
use my\Security;
use think\Db;
use JPush\Client as JPush;

//城际加密返回
function des_encode($str)
{
    $des = JoDES::share();
    $jm = '["' . $des->encode($str, "xmcx0551") . '"]';
    return $jm;
}

function des_decode($str)
{
    $des = JoDES::share();
    $de = $des->decode($str, "xmcx0551");
    if (empty($de) || $de === false) {
        return array('status' => 0, 'msg' => '解析失败');
    }
    $de = urldecode($de);
    $de_arr = explode("&", $de);//array("a=1","b=2","c=3")
    foreach ($de_arr as $val) {
        $val = explode("=", $val);
        $data[$val[0]] = $val[1];
    }
    foreach ($data as $k => $v) {
        $v = htmlspecialchars($v);
        $v = addslashes($v);
//        $v = str_replace("(", "", $v);
//        $v = str_replace(")", "", $v);
        $data[$k] = $v;
    }
    return array('status' => 1, 'data' => $data);
}


//plateColor车牌颜色JT/T415-2006
function plate_color()
{
    return array(1 => '蓝色', 2 => '黄色', 3 => '黑色', 4 => '白色', 5 => '绿色', 9 => '其他');
}

//fuelType 车辆燃料类型
function fuel_type()
{
    return array(
        'A' => '汽油', 'B' => '柴油', 'C' => '电', 'D' => '混合油', 'E' => '天然气',
        'F' => '液化石油气', 'L' => '甲醇', 'M' => '乙醇', 'N' => '太阳能', 'O' => '混合动力',
        'Y' => '无', 'Z' => '其它'
    );
}
//fixState 车辆检修状态
function fix_state()
{
    return array(0=>'未检修',1=>'已检修',2=>'未知');
}

//checkState车辆年度审核状态
function check_state()
{
    return array(0 => '未年审', 1 => '年审合格', 2 => '年审不合格');
}

//性别代码GB/T2261.1
function gender()
{
    return array(0=>'未知的性别',1=>'男',2=>'女',9=>'未说明的性别');
}

//marital_status 婚姻状态GB/T2261.1
function marital_status()
{
    return array(10=>'未婚',20=>'已婚',21=>'初婚',22=>'再婚',23=>'复婚',30=>'丧偶',40=>'离婚',90=>'未说明的婚姻状况');
}

/*GB/T 4658-2006学历代码*/
function education_code()
{
    return array(
        10 => '研究生教育', 11 => '博士研究生毕业', 12 => '博士研究生结业', 13 => '博士研究生肄业', 14 => '硕士研究生毕业',
        15 => '硕士研究生结业', 16 => '硕士研究生肄业', 17 => '研究生班毕业', 18 => '研究生班结业', 19 => '研究生班肄业',
        20 => '大学本科教育', 21 => '大学本科毕业', 22 => '大学本科结业', 23 => '大学本科肄业', 28 => '大学普通班毕业',
        30 => '大学专科教育', 31 => '大学专科毕业', 32 => '大学专科结业', 33 => '大学专科肄业', 40 => '中等职业教育',
        41 => '中等专科毕业', 42 => '中等专科结业', 43 => '中等专科肄业', 44 => '职业高中毕业', 45 => '职业高中结业',
        46 => '职业高中肄业', 47 => '技工学校毕业', 48 => '技工学校结业', 49 => '技工学校肄业', 50 => '高中以下',
        60 => '普通高级中学教育', 61 => '普通高中毕业', 62 => '普通高中结业', 63 => '普通高中肄业', 90 => '其他',
        91 => '中等师范学校（幼儿师范学校）毕业', 92 => '中等师范学校（幼儿师范学校）结业',
        93 => '中等师范学校（幼儿师范学校）肄业',
    );
}

/*JT/T 697.7-2014 5.16车型代码*/
function driver_type()
{
    return array(
        "A1" => '大型客车', "A2" => '牵引车', "A3" => '城市公交车', "B1" => '中型客车', "B2" => '大型货车',
        "C1" => '小型汽车', "C2" => '小型自动挡汽车', "C3" => '低速载货汽车', "C4" => '三轮汽车',
        "C5" => '残疾人专用小型自动挡载客汽车', "D" => '普通三轮摩托车', "E" => '普通二轮摩托车', "F" => '轻便摩托车',
        "M" => '轮式自行机械车', "N" => '无轨电车', "P" => '有轨电车',
    );
}

//
function commercial_type()
{
    return array(
        1 => '网络预约出租汽车', 2 => '巡游出租汽车', 3 => '私人小客车合乘'
    );
}

//手机运营商
function net_type(){
    return array(1=>'联通',2=>'移动',3=>'电信',4=>'其它');
}

//地图类型
function map_type(){
    return array(1=>'百度',2=>'高德',3=>'其它');
}

/**
 * 给司机推送信息
 * @param $info array('type'=>'信息标记driver|user','registration_id'=>'极光id','driver_id'=>'','uid'=>'','auth_status'=>'')
 * @param $title
 * @param $content
 * @param $type //1->用户端，2->城内司机，3->城际司机
 * @return array|false
 * */
function sendjg($info, $title, $content, $type = 1)
{
    return 1;
    if (!in_array($info['type'], array('driver', 'user'))) {
        return array('status' => 0, "msg" => '信息标记识别失败');
    }
    $id = $info['type'] == 'driver' ? $info['driver_id'] : $info['uid'];
    $auth_status = $info['type'] == 'driver' ? $info['auth_status'] : 'user';
    $send_type = $info['sendType'] ? $info['sendType'] : '';
    switch ($type) {
        case 1:
            $jpush = new JPush(config('jg_user_appKey'), config('jg_user_masterSecret'));
            break;
        case 2:
            $jpush = new JPush(config('jg_cn_appKey'), config('jg_cn_masterSecret'));
            break;
        case 3:
            $jpush = new JPush(config('jg_cj_appKey'), config('jg_cj_masterSecret'));
            break;
    }

    $platform = 'all';
    $alert = $content;
    // $sound_ios = $type==3?'new_order.caf':'';
    // $sound_and = $type==3?'new_order.mp3':'';
    $ios_notification = array(
        'sound' => '',//$sound_ios,
        //'badge' => 2,
        'content-available' => true,
        'category' => 'jiguang',
        'extras' => array(
            'key'       => 'value',
            'send_type' =>$send_type,
            'type'      => $send_type,
            'common_id'  => !empty($info['common_id'])?$info['common_id']:'',
            'jiguang'
        ),
    );
    $android_notification = array(
        'title' => $title,
        //'build_id' => 2,
        'extras' => array(
            'sound'     => '',//$sound_and,
            'key'       => 'value',
            'send_type' =>$send_type,
            'type'      => $send_type,
            'common_id'  => !empty($info['common_id'])?$info['common_id']:'',
            'jiguang'
        ),
    );
    $message = array(
        'title' => $title,
        'content_type' => 'text',
        'extras' => array(
            'id' => $id,
            'auth_status' => $auth_status,
            'send_type'=>$send_type,
            'type'      => $send_type,
            'common_id'  => !empty($info['common_id'])?$info['common_id']:'',
            'jiguang',
        ),
    );
    $options = array(
        'sendno' => $id,
    );
    $push_payload = $jpush->push()
        ->setPlatform($platform)
        ->addRegistrationId($info['registration_id'])
        ->iosNotification($alert, $ios_notification)
        ->androidNotification($alert, $android_notification)
//        ->message($content, $message)
        ->options($options);

    try {
        $response = $push_payload->send();
        return $response;
    } catch (\JPush\Exceptions\APIConnectionException $e) {
        Tools::dataLog($e,'TryConnection',6);
    } catch (\JPush\Exceptions\APIRequestException $e) {
        Tools::dataLog($e,'TryRequest',6);
    }
}

//测试用记录数据
function data_log($data, $type = 'send', $data_type = 1, $key = 'xmcx0551')
{
    if (empty($data)) {
        return false;
    }
    $des = JoDES::share();
    $content['request_time'] = date('Y-m-d H:i:s');
    $content['OS'] = Tools::mobileOS();
    $content['WEB'] = '/' . MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;
    switch ($type) {
        case 'send':
            $content['send_decode'] = $data;
            $content['send_encode'] = '["' . $des->encode(json_encode($data), $key) . '"]';
            $data_['type'] = 1;
            break;
        case 'receive':
            $content['receive_decode'] = $des->decode($data, $key);
            $content['receive_encode'] = $data;
            $data_['type'] = 2;
            break;
        default:
            $content[$type . '_decode_1'] = $content[$type . '_encode_2'] = $data;
            $content[$type . '_decode_2'] = $des->decode($data, $key);
            $content[$type . '_encode_1'] = '["' . $des->encode(json_encode($data), $key) . '"]';
            $data_['type'] = $data_type;
            break;
    }
    $data_['content'] = json_encode($content);
    Db::table('xm_transfer_data_record')->add($data_);
    return true;
}

//加密数据
function des_encrypt($str)
{
    if (empty($str)) {
        return false;
    }
    $des = JoDES::share();
    return $des->encode($str, "xmcx0551");
}

//解密数据
function des_decrypt($str)
{
    if (empty($str)) {
        return false;
    }
    $des = JoDES::share();
    return $des->decode($str, "xmcx0551");
}

/**
 * 处理城际数据二维数组
 * @param $data
 * @param $type //城际的司机和用户的加密方法和字段都不一致
 * @return array|false
 * */
function decode_array_inter_2($data, $type = 'driver')
{
    if (!empty($data)) {
        $encode_field = $type == 'driver' ? config('inter_encode_field') : config('encode_field');
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                if (in_array($k, $encode_field)) {
//                    $data[$key][$k] = $type == 'driver' ? des_decrypt($v) : Security::decrypt($v); 废弃des_decrypt()加密方法
                    $data[$key][$k] = Security::decrypt($v);
                } elseif (strpos($k, 'time') !== false) {
                    $data[$key][$k] = $v ? date('Y-m-d H:i:s', $v) : '';
                } elseif (strpos($k, '_at') !== false) {
                    $data[$key][$k] = $v ? date('Y-m-d', $v) : '';
                } elseif ($k == 'auth_status') {
                    $data[$key][$k] = auth_status()[$v];
                } elseif ($k == 'auth_type') {
                    $data[$key][$k] = auth_type()[$v];
                }elseif ($k == 'order_status') {
                    $data[$key][$k] = inter_order_status()[$v];
                } elseif ($k == 'paytool' || $k == 'pay_type') {
                    $data[$key][$k] = pay_type()[$v];
                } elseif($k=='order_type'){
                    $data[$key][$k] = order_type()[$v];
                }elseif($k=='plate_color'){
                    $data[$key][$k] = plate_color()[$v];
                }elseif($k=='operate_type'){
                    $data[$key][$k] = operate_type()[$v];
                }
                if ($v === null || $v === '') {
                    $data[$key][$k] = '';
                }
            }
        }
        return $data;
    }
    return false;
}

/**
 * 处理城际数据一维数组
 * @param $data
 * @param $type //城际的司机和用户的加密方法和字段都不一致
 * @return array|false
 * */
function decode_array_inter($data, $type = 'driver')
{
    if (!empty($data)) {
        $encode_field = $type == 'driver' ? config('inter_encode_field') : config('encode_field');
        foreach ($data as $key => $value) {
            if (in_array($key, $encode_field)) {
//                $data[$key] = $type == 'driver' ? des_decrypt($value) : Security::decrypt($value);  //des_decrypt()加密方法已废弃
                $data[$key] = Security::decrypt($value);
            } elseif (strpos($key, 'time')) {
                $data[$key] = $value ? date('Y-m-d H:i:s', $value) : '';
            } elseif (strpos($key, '_at')) {
                $data[$key] = $value ? date('Y-m-d', $value) : '';
            } elseif ($key == 'auth_status') {
                $data[$key] = auth_status()[$value];
            } elseif ($key == 'auth_type') {
                $data[$key] = auth_type()[$value];
            }elseif ($key == 'order_status') {
                $data[$key] = inter_order_status()[$value];
            }elseif ($key == 'paytool' || $key == 'pay_type') {
                $data[$key] = pay_type()[$value];
            }elseif($key=='order_type'){
                $data[$key] = order_type()[$value];
            }elseif($key=='plate_color'){
                $data[$key] = plate_color()[$value];
            }elseif($key=='operate_type'){
                $data[$key] = operate_type()[$value];
            }
            if ($value === null || $value === '') {
                $data[$key] = '';
            }
        }
        return $data;
    }
    return false;
}

function get_inter_driver_detail_column()
{
    return array(
        'driver_name' => '姓名',
        'driver_phone' => '联系方式',
        'driver_avatar' => '头像',
        'auth_status' => '认证状态',
        'id_number' => '身份证号码',
        'work_province' => '认证区域',
        'auth_type' => '认证类型',
        'last_login_time' => '上次登录时间',
        'register_time' => '注册时间',
        'register_ip' => '注册IP',
        'register_port' => '注册端口',
        'register_city' => '注册城市',
        'register_MAC' => '移动终端硬件标识',
        'register_IMSI' => '国际移动用户识别码',
        'register_IMEI' => '国际移动设备身份码',
    );
}

function get_inter_car_detail_column()
{
    return array(
//        'driver_name'=>'姓名',
//        'driver_phone'=>'联系方式',
//        'auth_status'=>'认证状态',
//        'id_number'=>'身份证号码',
//        'work_province'=>'认证区域',
//        'auth_type'=>'认证类型',
        'car_plate' => '车牌',
        'car_brand' => '车辆品牌',
        'car_model' => '车辆型号',
        'car_color' => '颜色',
        'owner' => '车辆所有人',
        'register_at' => '车辆注册时间',
//        'plate_color' => '车牌颜色',
        'seats' => '座位数',
//        'engine_id' => '发动机型号',
        'vin' => '车架号',

    );
}

//审核时需要添加的车辆信息
function auth_info_car()
{
    return array(
        'car_base' => array(
            'need' => array(
                'address' => '注册地行政区域代码',
                'plate_color' => '车牌颜色',
                'engine_id' => '发动机型号',
                'vin' => '车架号',
                'fuel_type' => '燃料类型',
                'engine_displace' => '发动机排量(毫升)',
                'certificate' => '运输证字号',
                'trans_agency' => '车辆运输证发证机构',
                'trans_area' => '车辆经营区域',
                'trans_date_start' => '运输证有效期起',
                'trans_date_stop' => '运输证有效期止',
                'certify_date_b' => '车辆初次登记日期',
                'fix_state' => '车辆检修状态',
                'fare_type' => '运价类型编码',
                'register_date' => '报备日期',
            ),
            'unNeed' => array(
                'next_fix_date' => '下次年检时间',
                'check_state' => '年度审验状态',
                'fee_print_id' => '发票打印设备序列号',
                'gps_brand' => 'GPS品牌',
                'gps_model' => 'GPS型号',
                'gps_imei' => 'GPS IMEI号',
                'gps_install_date' => 'GPS安装日期',

            ),
        ),
        'insurance' => array(
            'unNeed' => array(
                'insurcom' => '保险公司名称',
                'insurnum' => '保单号',
                'insurtype' => '保险类型',
                'insurcount' => '保险金额',
                'insureff' => '保险生效时间',
                'insurexp' => '保险到期时间',
            ),
        ),
    );
}

//审核时需要添加的司机信息
function auth_info_driver()
{
    return array(
        'driver_base' => array(
            'need' => array(
                'driver_sex' => '性别',
                'address' => '注册地行政区域代码',
                'driver_birthday' => '司机生日',
                'driver_nationality' => '国籍',
                'driver_nation' => '民族',
                'driver_marital_status' => '婚姻状况',
                'driver_language_level' => '外语能力',
                'driver_education' => '学历',
                'driver_census' => '户口登记机关名称',
                'driver_address' => '户口住址/常住地址',
                'driver_contact_address' => '联系地址',
                'license_id' => '机动车驾驶证号',
                'driver_type' => '准驾车型',
                'get_driver_license_date' => '初次领取驾驶证日期',
                'driver_license_on' => '驾驶证有效期起',
                'driver_license_off' => '驾驶证有效期止',
                'taxi_driver' => '巡游出租汽车驾驶员',
                'certificate_no' => '网络预约出租汽车驾驶员资格证号',
                'network_car_issue_organization' => '网约车驾驶员证发证机构',
                'network_car_issue_date' => '资格证发证日期',
                'get_network_car_proof_date' => '初次领取资格证日期',
                'network_car_proof_on' => '资格证有效期起始日期',
                'network_car_proof_off' => '资格证有效期截止日期',
                'register_date' => '报备日期',
                'full_time_driver' => '专职驾驶员',
                'in_driver_black_list' => '驾驶员黑名单',
                'commercial_type' => '服务类型',
            ),
            'unNeed'=>array(
                'contract_company' => '驾驶员合同签署公司',
                'contract_on' => '合同有效期起',
                'contract_off' => '合同有效期止',
                'emergency_contact' => '紧急情况联系人',
                'emergency_contact_phone' => '紧急情况联系人电话',
                'emergency_contact_address' => '紧急情况联系人通信地址',
            ),
        ),
        'app' => array(
            'unNeed' => array(
                'net_type' => '手机运营商',
                'app_version' => '使用APP版本号',
                'map_type' => '使用地图类型',
            )
        ),
        'course' => array(
            'unNeed' => array(
                'course_name' => '培训课程名称',
                'course_date' => '培训日期',
                'start_time' => '培训开始时间',
                'stop_time' => '培训结束时间',
                'duration' => '培训时长',
            )
        ),
    );
}

//审核时的选择字段
function auth_select_field()
{
    return array(
        'plate_color','fuel_type','fix_state','check_state','driver_sex','driver_marital_status','driver_education',
        'driver_type', 'taxi_driver','full_time_driver','in_driver_black_list','commercial_type','net_type','map_type',
    );
}

//城际订单状态
function inter_order_status()
{
    return array(
        0=> "待支付",10=>"超时取消",11=>'因故取消',12=>'有责取消',15=>"用户取消",16=>'客服取消',20=>"退款中",25=>"退款成功",30=>"等待出发",35=>"司机已接单",36=>"司机沿途接人",37=>'司机正在路上',38=>"司机到达乘客起点",40=>"已上车",41=>'行程中',45=>"司机已送达",47=>"乘客已到达",50=>"已评价");
}

function operate_type()
{
    return array(
        1=> "分配司机",2=>"取消分配"
    );
}
